//
//  LeftPandectViewController.m
//  Delicious2
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "LeftPandectViewController.h"

@interface LeftPandectViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) MBProgressHUD *HUD;

@property (nonatomic, strong) NSArray *arrayUp;
@property (nonatomic, strong) NSArray *arrayDown;

@end

@implementation LeftPandectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.page = 1;
    self.order_by = 1;
    self.order_asc = 0;
    
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    [self getData];
    [self creatTableView];
    [self creatButtons];
    self.navigationItem.rightBarButtonItem = nil;
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
  
}

#pragma mark - 创建排序按钮
- (void)creatButtons {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, 40)];
    CGFloat width = WIDTH / 4;
    CGFloat height = view.frame.size.height;
    self.arrayUp = @[@"常见", @"热量↑", @"蛋白质↑", @"脂肪↑"];
    self.arrayDown = @[@"常见", @"热量↓", @"蛋白质↓", @"脂肪↓"];
    for (int i = 0; i < 4; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(width * i, 0, width, height);
        button.backgroundColor = [UIColor clearColor];
        //对应高亮
        if (i == 0) {
            button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
            [button setTitleColor:kColor forState:UIControlStateNormal];
        } else {
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
        [button.titleLabel nightMode];
        button.tag = 1001 + i;

        [button setTitle:self.arrayUp[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonPaixuDidPress:) forControlEvents:UIControlEventTouchUpInside];
        
        [view addSubview:button];
    }
    [self.view addSubview:view];
}

#pragma mark - 建立tableView
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, WIDTH, self.view.frame.size.height - 40) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[LeftPandectCustomTableViewCell class] forCellReuseIdentifier:@"LeftPandectCustomTableViewCellIdentifier"];
    
    //上拉加载
    self.tableView.mj_footer = [MJChiBaoZiFooter footerWithRefreshingTarget:self refreshingAction:@selector(getData)];

    
    [self.view addSubview: self.tableView];
}

#pragma mark - tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LeftPandectCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LeftPandectCustomTableViewCellIdentifier"];
    if (self.dataSource.count != 0) {
        CookDetaileModel *detaileModel = self.dataSource[indexPath.row];
        cell.detaileModel = detaileModel;
        [cell nightMode];
    }
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    //跳转详情页面
    CookDetaileModel *detailModel = self.dataSource[indexPath.row];
    CookDetailsViewController *detaileVC = [[CookDetailsViewController alloc] init];
    detaileVC.strCode = detailModel.code;
    
    [self.navigationController pushViewController:detaileVC animated:YES];
}

//动画
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    cell.alpha = 0.5;
    
    CGAffineTransform transformScale = CGAffineTransformMakeScale(0.8, 0.8);
    CGAffineTransform transformTranslate = CGAffineTransformMakeTranslation(0.8, 0.8);
    cell.transform = CGAffineTransformConcat(transformScale, transformTranslate);
    
    [tableView bringSubviewToFront:cell];
    [UIView animateWithDuration:.4f delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        cell.alpha = 1;
        cell.transform = CGAffineTransformIdentity;
    } completion:nil];
    
}


#pragma mark - 获取数据
- (void)getData {
    
    NSString *strUrl = [NSString stringWithFormat:kPandectGETURL, self.strKind, self.value, self.order_by, self.page, self.order_asc];
    NSLog(@"%@", strUrl);
    [FSDNetWorkTool getWithURL:strUrl withParameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        NSArray *arr = result[@"foods"];
        
        for (NSDictionary *dic in arr) {
            CookDetaileModel *detaileModel = [[CookDetaileModel alloc] initWithDictionary:dic];
            [self.dataSource addObject:detaileModel];
        }
        
        [self.tableView reloadData];
        
        self.HUD.hidden = YES;
        
        [self.tableView.mj_footer endRefreshing];
        self.page++;
        
    } fail:^(NSError *error) {
        self.HUD.labelText = @"加载失败";
        [self.HUD hide:YES afterDelay:2];
        
       [self.tableView.mj_footer endRefreshing];
        NSLog(@"错误");
    }];
}

//排序按钮点击事件
- (void)buttonPaixuDidPress:(UIButton *)button {
    
    for (int i = 0; i < 4; i++) {
        UIButton *button = (UIButton *)[self.view viewWithTag: 1001 + i];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    self.page = 1;
    [self.dataSource removeAllObjects];

    NSInteger tag = button.tag % 1000 - 1;
    self.order_by = tag;
    
    if (self.order_asc == 0) {
        self.order_asc = 1;
        [button setTitle:self.arrayDown[tag] forState:UIControlStateNormal];
    } else {
        self.order_asc = 0;
        [button setTitle:self.arrayUp[tag] forState:UIControlStateNormal];
    }
    
    [self getData];
    
    button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [button setTitleColor:kColor forState:UIControlStateNormal];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.view nightMode];
    [self.tableView nightMode];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
